package com.orcs.leefei.utils.kotlin

import android.content.Context
import android.content.res.Configuration
import android.provider.Settings.Secure
import android.support.v4.content.ContextCompat.checkSelfPermission
import android.support.v4.content.PermissionChecker.PERMISSION_GRANTED
import android.telephony.TelephonyManager
import com.orcs.leefei.utils.R
import com.orcs.leefei.utils.kotlin.Orcs.resources


/**
 *
 * Created by FeiLee on 2017/1/12 18:24.
 */
fun getAndroidId(): String? {
    val permission = checkSelfPermission(Orcs.context, "android.permission.READ_PHONE_STATE")
    if (permission === PERMISSION_GRANTED) {
        return Secure.getString(Orcs.context.contentResolver, Secure.ANDROID_ID)
    }

    return null
}

fun getDeviceId(): String? {
    val permission = checkSelfPermission(Orcs.context, "android.permission.READ_PHONE_STATE")
    if (permission === PERMISSION_GRANTED) {
        val tm = Orcs.context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return tm.deviceId;
    }

    return null
}

fun getSimSerialNumber(): String? {
    val permission = checkSelfPermission(Orcs.context, "android.permission.READ_PHONE_STATE")
    if (permission === PERMISSION_GRANTED) {
        val tm = Orcs.context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return tm.simSerialNumber;
    }

    return null
}

fun getSystemVersion(): String? = android.os.Build.VERSION.RELEASE

fun isLandscape() =
        (resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE)

fun isPortrait() =
        (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT)


fun isTablet(): Boolean {
    return (resources.configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK) >=
            Configuration.SCREENLAYOUT_SIZE_LARGE
}


fun isLargeTablet() = Orcs.resources.getBoolean(R.bool.large_tablet)